//
//  TopicViewController.m
//  美食百科
//
//  Created by dllo on 16/1/29.
//  Copyright © 2016年 doll. All rights reserved.
//

#import "TopicViewController.h"
#import "NewWorking.h"
#import "CarouselModel.h"
#import "TopicCollectionViewCell.h"
#import "InforViewController.h"
#import "Reachability.h"
#define kWidth self.view.frame.size.width
#define kHeight self.view.frame.size.height
@interface TopicViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(nonatomic,retain)NSMutableArray *array;
@property(nonatomic,retain)UICollectionView *collection;
@property(nonatomic,retain)UIPageControl *page;
@end

@implementation TopicViewController
- (void)dealloc
{
    [_collection release];
    [_array release];
    [_page release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   // self.view.backgroundColor = [UIColor whiteColor];
    
   // self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    self.navigationController.navigationBarHidden = YES;
   // self.navigationController.navigationBar.translucent = NO;
   
    
    NSLog(@"🍎%@",self.Id);

    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.9 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        Reachability *reach = [Reachability reachabilityForInternetConnection];
        
        //如果无网络,弹窗提示
        if ( 0 == reach.currentReachabilityStatus){
            //弹窗提示
            UILabel *nonconnectionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, -50, kWidth, 50)];
            nonconnectionLabel.textAlignment = NSTextAlignmentCenter;
            nonconnectionLabel.backgroundColor = [UIColor redColor];
            nonconnectionLabel.text = @"网络不给力,刷新试试~";
            nonconnectionLabel.textColor = [UIColor whiteColor];
            [self.view addSubview:nonconnectionLabel];
            
            //动画效果,通知栏缓慢出现
            [UIView animateWithDuration:0.6 animations:^{
                nonconnectionLabel.frame = CGRectMake(0, 0, kWidth, 50);
            } completion:^(BOOL finished) {
                [UIView animateWithDuration:0.6 delay:3 options:UIViewAnimationOptionAutoreverse animations:^{
                    nonconnectionLabel.frame = CGRectMake(0, -50, kWidth, 50);
                    [nonconnectionLabel release];
                    
                } completion:^(BOOL finished) {
                    
                }];
                
                //            [UIView animateWithDuration:1.9 animations:^{
                //                nonconnectionLabel.frame = CGRectMake(0, -50, kWidth, 50);
                //            //[nonconnectionLabel removeFromSuperview];
                //                [nonconnectionLabel release];
                //            }];
                
            }];
            
        }
    });

    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(15, 20, 15, 15);
    [backButton setBackgroundImage:[UIImage imageNamed:@"iconfont-zuojiantou-3"]forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
//    UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    shareButton.frame = CGRectMake(kWidth-30, 20, 15, 15);
//    [shareButton setBackgroundImage:[UIImage imageNamed:@"share"]forState:UIControlStateNormal];
//    [shareButton addTarget:self action:@selector(shareAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:shareButton];
    self.array = [NSMutableArray array];
    [NewWorking GetDayaWithURL:[NSString stringWithFormat:@"http://food.boohee.com/fb/v1/topics/%@",self.Id] dic:nil sussess:^(id responseObject) {
       
        for (NSDictionary *dic in [responseObject[@"topic"] objectForKey:@"pages"]) {
            CarouselModel *carousel = [[CarouselModel alloc]initWithDictionary:dic];
            
            if (carousel.food_code == nil){
                
            }else
            {
                [self.array addObject:carousel];

            }
            
        }
        
        NSLog(@"🐌%@",self.array);
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
       // layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        layout.minimumLineSpacing = 0;
        //layout.minimumInteritemSpacing = 0;
        layout.itemSize = CGSizeMake(kWidth, kHeight);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        self.collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight) collectionViewLayout:layout];
        self.collection.backgroundColor = [UIColor clearColor];
        self.collection.dataSource = self;
        self.collection.delegate  = self;
        self.collection.pagingEnabled = YES;
        self.collection.bounces = NO;
        [self.view addSubview:self.collection];
        self.collection.showsHorizontalScrollIndicator = NO;
        [self.collection addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:@"self.collection的偏移量"];
        [layout release];
        [_collection release];
        
        [self.collection registerClass:[TopicCollectionViewCell class] forCellWithReuseIdentifier:@"topic"];
        
        self.page = [[UIPageControl alloc]initWithFrame:CGRectMake(0, kHeight-30, kWidth, 30)];
        self.page.backgroundColor = [UIColor whiteColor];
        self.page.numberOfPages = self.array.count;
        self.page.currentPageIndicatorTintColor = [UIColor lightGrayColor];
        self.page.pageIndicatorTintColor = [UIColor colorWithRed:0.945 green:0.945 blue:0.945 alpha:1.00];
        [self.page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
        
        [self.view addSubview:self.page];
        [_page release];
    
        [self.view bringSubviewToFront:backButton];
        //[self.view bringSubviewToFront:shareButton];
    } filed:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
    
    
    
    
    
    
    
    
    
    
}


//-(void)shareAction:(UIButton *)button
//{
//    
//}

-(void)pageAction:(UIPageControl *)page
{
    self.collection.contentOffset = CGPointMake(self.page.currentPage*kWidth, 0);
}

-(void)backAction:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

//self.page与偏移量的联系
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    self.page.currentPage =  [[change objectForKey:@"new"] CGPointValue].x/kWidth;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.array.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TopicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"topic" forIndexPath:indexPath];
    cell.carousel = self.array[indexPath.row];
    NSLog(@"%@",cell.carousel.content);
    
    //跳转详情页
    cell.block = ^(CarouselModel *carousel)
    {InforViewController *infor = [[InforViewController alloc]init];
        infor.carouse  = carousel;
        infor.number =2;
        [self.navigationController pushViewController:infor animated:YES];
        
        self.navigationController.navigationBarHidden = NO;
        [infor release];
    };
//cell.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255 blue:arc4random()%256/255 alpha:1];
    return cell;
}


//-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//
//
//CarouselModel *carousel = self.array[indexPath.row];
//    NSLog(@"%@",carousel.content);
//  CGFloat height = [TopicCollectionViewCell heightOfSelfSuit:carousel.content font:[UIFont systemFontOfSize:17] width:kWidth-20];
//    NSLog(@"%f",height+10+kHeight/5*2+40+30);
//    
//    return CGSizeMake(kWidth, height+10+kHeight/5*2+40+30);
//    
//}

- (BOOL)prefersStatusBarHidden{
    NSLog(@"ne");
    return YES;
    
}
-(void)viewDidAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
